package com.hetongxue.system.controller;

import cn.hutool.core.map.MapUtil;
import com.hetongxue.lang.Const;
import com.hetongxue.response.Result;
import com.hetongxue.system.domain.vo.LoginVo;
import com.hetongxue.system.service.UserService;
import com.hetongxue.utils.CaptchaUtil;
import com.hetongxue.utils.HttpUtil;
import com.hetongxue.utils.RedisUtil;
import com.wf.captcha.base.Captcha;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 权限控制器
 * @ClassNmae: AuthController
 * @Author: 何同学
 * @DateTime: 2022-04-26 13:37
 **/
@RestController
@RequiredArgsConstructor
@Api("安全模块")
public class AuthController {

    private final RedisUtil redisUtil;
    private final UserService userService;

    /**
     * 获取验证码
     **/
    @GetMapping("/getVerify")
    public Result getVerify() {
        Captcha captcha = CaptchaUtil.getCaptcha();
        HttpUtil.getSession().setAttribute(Const.CAPTCHA_KEY, captcha.text());
        // 存入redis  这里先使用session进行验证 后期在更换
        return Result.Success(
                MapUtil.builder()
                        .put("key", Const.CAPTCHA_KEY)
                        .put("imgBase64", captcha.toBase64())
                        .build());
    }

    /**
     * 校验验证码
     **/
    @GetMapping("/checkVerify")
    public Result checkVerify(String code) {
        return HttpUtil.getRequest().getSession().getAttribute(Const.CAPTCHA_KEY).toString().trim().equals(code.trim()) ?
                Result.Success(true).setMessage("验证成功") : Result.Error(false).setMessage("验证码错误");
    }

    /**
     * 登陆实现
     **/
    @PostMapping("/loginCheck")
    public Result loginCheck(@RequestBody LoginVo loginVo) {
//        User userinfo = userService.login(loginVo);
        return Result.Success(loginVo);
//        return userinfo != null ? Result.Success(userinfo) : Result.Error().setMessage("用户名或密码错误");
    }

}
